package me.xiaoyan.android.widget;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.text.TextUtils;

public class YanContact {

	/** 获取库Phon表字段 **/
	private static final String[] PHONES_PROJECTION = new String[] {
			Contacts.DISPLAY_NAME, Contacts.PHOTO_ID, Contacts._ID,
			Contacts.HAS_PHONE_NUMBER };

	/** 联系人显示名称 **/
	private static final int PHONES_DISPLAY_NAME_INDEX = 0;

	/** 头像ID **/
	private static final int PHONES_PHOTO_ID_INDEX = 1;

	/** 联系人的ID **/
	private static final int PHONES_CONTACT_ID_INDEX = 2;

	/** 联系人 号码数 **/
	private static final int PHONE_NUMBER_COUNT_INDEX = 3;

	private ContentResolver resolver;

	public YanContact(Context context) {
		resolver = context.getContentResolver();
	}

	public List<Contactor> QueryAll() {
		// 获取手机联系人
		Cursor phoneCursor = resolver.query(Contacts.CONTENT_URI,
				PHONES_PROJECTION, null, null, null);

		return fetchCursor(phoneCursor);
	}

	public List<Contactor> Search(String searchKey) {
		if (TextUtils.isEmpty(searchKey)) {
			searchKey = "%%";
		} else {
			char[] cs = searchKey.toCharArray();
			StringBuilder sb = new StringBuilder("%");
			for (char c : cs) {
				sb.append(c + "%");
			}
			searchKey = sb.toString();
		}
		Cursor phoneCursor = resolver.query(Contacts.CONTENT_URI,
				PHONES_PROJECTION, "sort_key like '" + searchKey +"'", null, Contacts.DISPLAY_NAME + " ASC");
		return fetchCursor(phoneCursor);
	}

	private List<Contactor> fetchCursor(Cursor phoneCursor) {
		List<Contactor> list = new ArrayList<Contactor>();
		if (phoneCursor != null) {
			while (phoneCursor.moveToNext()) {

				Vector<String> phones = new Vector<String>();

				// 得到联系人ID
				Long contactId = phoneCursor.getLong(PHONES_CONTACT_ID_INDEX);

				int phoneCount = phoneCursor.getInt(PHONE_NUMBER_COUNT_INDEX);
				// 当没有手机号码时 读取下一个
				if (phoneCount < 1) {
					continue;
				} else {
					Cursor numberCursor = resolver.query(Phone.CONTENT_URI,
							null, Phone.CONTACT_ID + "=" + contactId, null,
							null);

					while (numberCursor.moveToNext()) {
						String phoneNumber = numberCursor
								.getString(numberCursor
										.getColumnIndex(Phone.NUMBER));
						phones.add(phoneNumber);
					}
				}

				// 得到联系人名称
				String contactName = phoneCursor
						.getString(PHONES_DISPLAY_NAME_INDEX);

				// 得到联系人头像ID
				Long photoid = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX);

				// 得到联系人头像Bitamp
				Bitmap contactPhoto = null;

				// photoid 大于0 表示联系人有头像 如果没有给此人设置头像则给他一个默认的
				if (photoid > 0) {
					Uri uri = ContentUris.withAppendedId(
							ContactsContract.Contacts.CONTENT_URI, contactId);
					InputStream input = ContactsContract.Contacts
							.openContactPhotoInputStream(resolver, uri);
					contactPhoto = BitmapFactory.decodeStream(input);
				} else {
					contactPhoto = null;
				}
				list.add(new Contactor(contactName, phones, contactPhoto));
			}
		}
		return list;
	}

	public class Contactor {
		public long id;
		public String name;
		public Vector<String> phoneNumbers;
		public Bitmap headPhoto;

		public Contactor() {
			super();
		}

		public Contactor(String name, Vector<String> phoneNumbers) {
			super();
			this.name = name;
			this.phoneNumbers = phoneNumbers;
		}

		public Contactor(String name, Vector<String> phoneNumbers,
				Bitmap headPhoto) {
			super();
			this.name = name;
			this.phoneNumbers = phoneNumbers;
			this.headPhoto = headPhoto;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public Vector<String> getPhoneNumbers() {
			return phoneNumbers;
		}

		public void setPhoneNumbers(Vector<String> phoneNumbers) {
			this.phoneNumbers = phoneNumbers;
		}

		public Bitmap getHeadPhoto() {
			return headPhoto;
		}

		public void setHeadPhoto(Bitmap headPhoto) {
			this.headPhoto = headPhoto;
		}
	}
}
